package com.whf.android.jar.util.storage;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.whf.android.jar.app.Latte;

/***
 * Created by 汪海峰 on 2017/4/22
 * @author 汪海峰
 */
public final class LattePreference {

    /**
     * 提示:
     * Activity.getPreferences(int mode)生成 Activity名.xml 用于Activity内部存储
     * PreferenceManager.getDefaultSharedPreferences(Context)生成 包名_preferences.xml
     * Context.getSharedPreferences(String name,int mode)生成name.xml
     */
    private static final SharedPreferences PREFERENCES =
            PreferenceManager.getDefaultSharedPreferences(Latte.getApplicationContext());
    private static final String APP_PREFERENCES_KEY = "profile";

    private static final String KEY_AES = "0123456789abcdef";

    private static final byte[] BYTES_KEY_AES = ConvertUtils.string2Bytes(KEY_AES);

    private static final String AES_ECB_PK_CS5_PADDING = "AES/ECB/PKCS5Padding";

    private static SharedPreferences getAppPreference() {
        return PREFERENCES;
    }

    public static void setAppProfile(String val) {
        getAppPreference()
                .edit()
                .putString(APP_PREFERENCES_KEY, val)
                .apply();
    }

    public static String getAppProfile() {
        return getAppPreference().getString(APP_PREFERENCES_KEY, null);
    }


    public static void removeAppProfile() {
        getAppPreference()
                .edit()
                .remove(APP_PREFERENCES_KEY)
                .apply();
    }

    public static void clearAppPreferences() {
        getAppPreference()
                .edit()
                .clear()
                .apply();
    }

    /************************ boolean *************************/

    public static void setAppFlag(String key, boolean flag) {
        setAppBool(key, flag);
    }

    public static void setAppBool(String key, boolean bool) {
        getAppPreference()
                .edit()
                .putBoolean(key, bool)
                .apply();
    }

    public static boolean getAppFlag(String key) {
        return getAppBool(key, false);
    }

    public static boolean getAppFlag(String key, boolean flag) {
        return getAppBool(key, flag);
    }

    public static boolean getAppBool(String key) {
        return getAppBool(key, false);
    }

    public static boolean getAppBool(String key, boolean bool) {
        return getAppPreference().getBoolean(key, bool);
    }


    /************************ int *************************/

    public static void setAppSign(String key, int flag) {
        setAppInt(key, flag);
    }

    public static void setAppInt(String key, int flag) {
        getAppPreference()
                .edit()
                .putInt(key, flag)
                .apply();
    }

    public static int getAppSign(String key) {
        return getAppInt(key, -1);
    }

    public static int getAppSign(String key, int val) {
        return getAppInt(key, val);
    }

    public static int getAppInt(String key) {
        return getAppInt(key, -1);
    }

    public static int getAppInt(String key, int val) {
        return getAppPreference().getInt(key, val);
    }

    /************************ long *************************/

    public static void setAppLong(String key, long flag) {
        getAppPreference()
                .edit()
                .putLong(key, flag)
                .apply();
    }

    public static long getAppLong(String key) {
        return getAppLong(key, -1);
    }

    public static long getAppLong(String key, long val) {
        return getAppPreference().getLong(key, val);
    }

    /************************ float *************************/

    public static void setAppFloat(String key, float flag) {
        getAppPreference()
                .edit()
                .putFloat(key, flag)
                .apply();
    }

    public static float getAppFloat(String key) {
        return getAppLong(key, -1);
    }

    public static float getAppFloat(String key, float val) {
        return getAppPreference().getFloat(key, val);
    }

    /************************ string *************************/

    public static void addCustomAppProfile(String key, String val) {
        getAppPreference()
                .edit()
                .putString(key, encrypt(val))
                .apply();
    }

    public static String getCustomAppProfile(String key) {
        return getCustomAppProfile(key, "");
    }

    public static String getCustomAppProfile(String key, String val) {
        try {
            String name = getAppPreference().getString(key, val);
            if (val.equals(name) || ObjectUtils.isEmpty(name)) {
                //如果没有存储值，使用的默认值，就不需要解密。
                return name;
            }
            return decrypt(name);
        } catch (Exception e) {
            return val;
        }
    }

    /*** AES 加密 */
    private static String encrypt(String dataAES) {
        if (ObjectUtils.isEmpty(dataAES)) {
            //如果存储值为空，就不需要加密。
            return dataAES;
        }
        final byte[] bytesDataAes = ConvertUtils.string2Bytes(dataAES);
        return EncryptUtils.encryptAES2HexString(bytesDataAes, BYTES_KEY_AES, AES_ECB_PK_CS5_PADDING, null);
    }

    /*** AES 解密 */
    private static String decrypt(String resAES) {
        byte[] bytesResAES = EncryptUtils.decryptHexStringAES(resAES, BYTES_KEY_AES, AES_ECB_PK_CS5_PADDING, null);
        return ConvertUtils.bytes2String(bytesResAES);
    }

}
